package autoconfigure.aspect;

import autoconfigure.config.SlowProperty;
import autoconfigure.anon.SlowService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.util.StopWatch;

/**
 * @author zengjw
 * @Description 慢查询切面实现
 * @CreateTime 2023-02-27
 **/
@Slf4j
@Aspect
@RequiredArgsConstructor
public class SlowQueryAspect {
    private final SlowProperty slowProperty;
    StopWatch stopWatch = new StopWatch();

    @Before("@annotation(slowService)")
    public void before(JoinPoint pjp, SlowService slowService) {
        stopWatch.start();
    }

    /**
     *
     * @param slowService
     */
    @After("@annotation(slowService)")
    public void after(JoinPoint pjp, SlowService slowService){
        stopWatch.stop();
        //耗时
        long totalTimeMillis = stopWatch.getTotalTimeMillis();
        if (totalTimeMillis>=slowProperty.getSlowMillons()){
            //记录日志
        }
    }
}
